Imports System.Data.SqlClient
Imports System.Drawing
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.XtraEditors
Imports DevExpress.XtraEditors.Repository

'--- Created Date On : 21-Oct-2010 (Jagdish)

Public Class frmChargesPaymentLogin
    Public VisitID As Integer
    Public PatientID As Integer
    Public PracticeId As Integer
    Public frmId As Integer
    Public strName As String
    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        Static intNumberofAttempts As Integer
        Dim strQuery As String
        Dim rdLogin As SqlDataReader
        Dim mCommand As SqlCommand
        gintPaymentUserID = 0
        Try

            strQuery = "SELECT UserId, FirstName + ' ' + LastName as Name,PassWord,UserLevelId,LoggedUsers,UserStatus,LoginName,PinNo FROM tblUsers WHERE LoginName='" & Trim(txtLoginID.Text) & "'"
            mCommand = New SqlCommand(strQuery, gconWebPlusMed)
            rdLogin = mCommand.ExecuteReader

            rdLogin.Read()

            If rdLogin.HasRows = False Then
                mCommand = Nothing
                rdLogin.Close() : rdLogin = Nothing
                If intNumberofAttempts = 2 Then
                    MessageBox.Show("Consult your system administrator", gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Me.Close()
                    Exit Sub
                End If
                MessageBox.Show("Invalid Login ID! Specify Again", gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                'lblLogin.Text = "Invalid Login ID! Specify Again"
                txtLoginID.Focus()
                txtPinNo.Text = ""
                intNumberofAttempts = intNumberofAttempts + 1
                Exit Sub
            ElseIf IsDBNullOrNothing(rdLogin.Item("PinNo")) Then
                MessageBox.Show("Pin No is not entered for this user. Please insert Pin No for this user before proceeding", gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                txtPinNo.Text = ""
                intNumberofAttempts = intNumberofAttempts + 1
                Exit Sub
            ElseIf TDecrypt(rdLogin.Item("PinNo")) <> Trim(txtPinNo.Text) Then
                mCommand = Nothing
                rdLogin.Close() : rdLogin = Nothing
                If intNumberofAttempts = 2 Then
                    MessageBox.Show("Consult your system administrator", gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Me.Close()
                    Exit Sub
                End If
                MessageBox.Show("Invalid Pin No! Specify Again", gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                txtPinNo.Focus()
                intNumberofAttempts = intNumberofAttempts + 1
                Exit Sub
            ElseIf rdLogin.Item("UserStatus") = False Then
                MessageBox.Show("Permission Denied" & vbCrLf & "Consult your system administrator", gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
                Me.Close()
                Exit Sub
            Else
                gintPaymentUserID = rdLogin.Item("UserId")
                'If frmId <> 0 Then
                '    'Dim IsLoaded As Boolean = False
                '    'For Each frmchild As Form In Me.ParentForm.MdiChildren
                '    '    If frmchild.Name.ToString = "frmChargeAndPayment" Then
                '    '        frmchild.BringToFront() : IsLoaded = True
                '    '    End If
                '    'Next
                '    'If IsLoaded = False Then
                '    Dim frmTempChargesAndPayment As New frmChargeAndPayment(VisitID, PatientID, PracticeId)
                '    frmTempChargesAndPayment.grbPayment.Enabled = True
                '    '    frmTempChargesAndPayment.MdiParent = Me.ParentForm
                '    '    frmTempChargesAndPayment.Show()
                '    'End If

                'Else
                '    mblnPopulateAccount = True
                'End If
                mblnPopulateAccount = True
                rdLogin.Close() : rdLogin = Nothing ' V1.1
            End If

            'rdLogin.Close() 'V1.1

            Me.Close()

        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
            'txtlogin.Text = "": txtpasswd.Text = "": txtlogin.SetFocus
        End Try
    End Sub

    Private Sub EnableOKButton()
        If txtLoginID.Text <> "" And txtPinNo.Text <> "" Then
            btnOk.Enabled = True
        Else
            btnOk.Enabled = False
        End If
    End Sub

    Private Sub frmChargesPaymentLogin_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        txtLoginID.Text = ""
        txtPinNo.Text = ""
        txtLoginID.Text = strRecordID("tblUsers", "LoginName", "UserId=" & gintUserID)
        txtPinNo.Focus()
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        gintPaymentUserID = 0
        Me.Close()
    End Sub

    Private Sub txtLoginID_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtLoginID.GotFocus
        txtLoginID.SelectAll()
    End Sub

    Private Sub txtPassword_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPinNo.GotFocus
        txtPinNo.SelectAll()
    End Sub

    Private Sub txtLoginID_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtLoginID.TextChanged
        Call EnableOKButton()
    End Sub

    Private Sub txtPassword_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPinNo.TextChanged
        Call EnableOKButton()
    End Sub
End Class
